package com.dhsc.util;

/**
 * @ClassName:  Sm4Util
 * @Description: SM4加密公共方法
 * @date:   2024年4月17日 下午5:13:40       
 * @author: clong
 * @Copyright:  东海运营
 */
public class Sm4Util {
	
	private Sm4Util()
	{

	}

	public static String encrypt(String val, String keys) {
		
		byte[] in = val.getBytes();
		byte[] key = keys.getBytes();
		SMS4 sm4 = new SMS4();
		byte[] input = sm4.paddingPCS5(in);
		byte[] out = new byte[input.length];
		int encrypt = 1;
		//加密 128bit
		sm4.sms4(input, input.length, key, out, encrypt);
		return byteArrToHex(out);	
	}
	
    private static final char[] hexArray = "0123456789abcdef".toCharArray();
    
    public static String byteArrToHex(byte... bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
	
}
